package com.powernode.driver.service.impl;


import com.powernode.driver.config.TencentProperties;
import com.powernode.driver.service.CosService;
import com.powernode.driver.service.OcrService;
import com.powernode.model.vo.driver.CosUploadVo;
import com.powernode.model.vo.driver.IdCardOcrVo;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import org.joda.time.format.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;


@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OcrServiceImpl implements OcrService {

    @Resource
    private TencentProperties tencentProperties;

    @Resource
    private CosService cosService;

    /**
     * 识别身份证图片信息
     */
    @Override
    public IdCardOcrVo idCardOcr(MultipartFile file) {
        try {
            //将图片转成base64 文本   本质将2进制数据转成文本数据
            byte[] encoder = Base64.encodeBase64(file.getBytes());
            String idCardBase64 = new String(encoder);

            //创建凭证
            Credential credential = new Credential(tencentProperties.getSecretId(), tencentProperties.getSecretKey());

            //创建ocrClient
            OcrClient ocrClient = new OcrClient(credential, tencentProperties.getRegion());

            //构建ocr请求对象
            IDCardOCRRequest req = new IDCardOCRRequest();
            req.setImageBase64(idCardBase64);

            //发送请求获取结果
            IDCardOCRResponse resp = ocrClient.IDCardOCR(req);

            IdCardOcrVo idCardOcrVo = new IdCardOcrVo();

            //判断正反面
            if (StringUtils.hasText(resp.getName())) {
                //正面
                idCardOcrVo.setName(resp.getName());
                idCardOcrVo.setGender("男".equals(resp.getSex()) ? "1" : "2");
                //生日 字符串转date
                Date birthday = DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime(resp.getBirth()).toDate();
                idCardOcrVo.setBirthday(birthday);
                idCardOcrVo.setIdcardNo(resp.getIdNum());
                idCardOcrVo.setIdcardAddress(resp.getAddress());

                //将身份证上传到cos中
                CosUploadVo cosUploadVo = cosService.upload(file, "idCard");

                idCardOcrVo.setIdcardFrontUrl(cosUploadVo.getUrl());
                idCardOcrVo.setIdcardFrontShowUrl(cosUploadVo.getShowUrl());
            }else{
                //获取过期时间 2010.01.01-2026.01.01
                String idCardExpire = resp.getValidDate().split("-")[1];
                Date cardExpireDate = DateTimeFormat.forPattern("yyyy.MM.dd").parseDateTime(idCardExpire).toDate();
                idCardOcrVo.setIdcardExpire(cardExpireDate);

                //反面存储到cos中
                CosUploadVo cosUploadVo = cosService.upload(file, "idCard");
                idCardOcrVo.setIdcardBackUrl(cosUploadVo.getUrl());
                idCardOcrVo.setIdcardBackShowUrl(cosUploadVo.getShowUrl());
            }

            return idCardOcrVo;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
    }
}
